package com.blog.admin.service;


import com.blog.admin.mapper.SysPermissionsMapper;
import com.blog.admin.mapper.SysUserMapper;
import com.blog.admin.vo.SysUserInfomationVO;
import com.blog.admin.vo.SysUserRoleVO;
import com.blog.admin.vo.SysUserVO;
import com.blog.util.PasswordUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.blog.util.DateUtil.getNowTime;

@Service
@Transactional
public class SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysPermissionsMapper sysPermissionsMapper;

    /**
     * 默认密码
     */
    public static String PASSWORD_VALUE = "123456";
    /**
     * 用户账号锁定
     */
    public static Integer USER_TYPE_LOCK = 1;

    /**
     * 插入用户信息
     *
     * @param sysUserVO
     * @return
     */
    public boolean insertUser(SysUserVO sysUserVO, SysUserInfomationVO sysUserInfomationVO) {
        sysUserVO.setCreateTime(getNowTime());
        sysUserInfomationVO.setCreateTime(getNowTime());
        sysUserInfomationVO.setUpdateTime(getNowTime());
        boolean result = true;
        try {
            if (StringUtils.isEmpty(sysUserVO.getPassword())){
                sysUserVO.setPassword(PasswordUtil.encrypt(PASSWORD_VALUE,sysUserVO.getUsername(),PasswordUtil.getStaticSalt()));
            }
            sysUserMapper.insertUser(sysUserVO);
            if (sysUserInfomationVO==null){
                sysUserInfomationVO = new SysUserInfomationVO();
            }
            if (!StringUtils.isEmpty(sysUserVO.getId())) {
                sysUserInfomationVO.setPkUser(sysUserVO.getId());
                sysUserMapper.insertInfomationVO(sysUserInfomationVO);
                SysUserRoleVO sysUserRoleVO = new SysUserRoleVO();
                sysUserRoleVO.setPkUser(sysUserVO.getId());
                if (sysUserVO.getSysRoleVO()!=null) {
                    sysUserRoleVO.setPkRole(sysUserVO.getSysRoleVO().getId());
                }
                sysPermissionsMapper.insertUserRole(sysUserRoleVO);
            } else {
                result = false;
            }
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /**
     * 获取用户列表
     *
     * @param page
     * @param size
     * @return
     */

    public PageInfo<SysUserVO> getUserList(int page, int size) {
        PageHelper.startPage(page, size);
        List<SysUserVO> list = sysUserMapper.getUserList();
        PageInfo<SysUserVO> pageRest = new PageInfo<SysUserVO>(list);
        return pageRest;
    }

    /**
     * 根据用户名查找用户
     *
     * @param username
     * @return
     */
    public SysUserVO getUserByUsername(String username) {
        SysUserVO result = sysUserMapper.getUserByUsername(username);
        return result;
    }

    /**
     * 修改用户信息
     *
     * @param sysUserVO
     * @return
     */
    public boolean updateUser(SysUserVO sysUserVO, SysUserInfomationVO sysUserInfomationVO) {
        boolean result = true;
        sysUserVO.setUpdateTime(getNowTime());
        sysUserInfomationVO.setUpdateTime(getNowTime());
        try {
            // 修改个人信息表
            if (StringUtils.isEmpty(sysUserInfomationVO.getPkUser())) {
                sysUserInfomationVO.setPkUser(sysUserVO.getId());
            }
            if (!StringUtils.isEmpty(sysUserInfomationVO.getPkUser())) {
                sysUserMapper.updateUserInfomation(sysUserInfomationVO);
            }
            //修改用户和用户角色表
           /* if (!StringUtils.isEmpty(sysUserVO.getId())) {
              //  sysUserMapper.updateUser(sysUserVO);
                SysUserRoleVO sysUserRoleVO = new SysUserRoleVO();
                sysUserRoleVO.setPkUser(sysUserVO.getId());
                sysUserRoleVO.setPkRole(sysUserVO.getSysRoleVO().getId());
                sysPermissionsMapper.updateUserRole(sysUserRoleVO);
            }*/
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /**
     * 修改账号状态
     *
     * @param sysUserVO
     * @return
     */
    public boolean updateUserType(SysUserVO sysUserVO) {
        boolean result = true;
        sysUserVO.setUpdateTime(getNowTime());
        try {
            sysUserMapper.updateUserType(sysUserVO);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /**
     * 重置密码
     * @param sysUserVO
     * @return
     */
    public boolean resetPassword(SysUserVO sysUserVO) {
        boolean result = true;
        sysUserVO.setUpdateTime(getNowTime());
        try {
            // 密码加密
            sysUserVO.setPassword(PasswordUtil.encrypt(PASSWORD_VALUE,sysUserVO.getUsername(),PasswordUtil.getStaticSalt()));
            sysUserMapper.updateUser(sysUserVO);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /**
     * 修改密码
     * @param sysUserVO
     * @return
     */
    public boolean updatePassword(SysUserVO sysUserVO) {
        boolean result = true;
        sysUserVO.setUpdateTime(getNowTime());
        try {
            // 密码加密
            sysUserVO.setPassword(PasswordUtil.encrypt(PASSWORD_VALUE,sysUserVO.getUsername(),PasswordUtil.getStaticSalt()));
            sysUserMapper.updateUser(sysUserVO);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /**
     * 根据id删除用户
     *
     * @param id
     * @return
     */
    public boolean deleteUserById(String id) {
        boolean result = true;
        try {
            SysUserRoleVO sysUserRoleVO = new SysUserRoleVO();
            sysUserRoleVO.setPkUser(id);
            sysPermissionsMapper.deleteUserRole(sysUserRoleVO);
            sysUserMapper.deleteUserById(id);
            sysUserMapper.deleteUserInfomationById(id);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

}
